{smcl}
{com}{sf}{ul off}{txt}{.-}
      name:  {res}<unnamed>
       {txt}log:  {res}C:\Users\hilar\Box\lights_2022\replication package\code\read_other_rasters.smcl
  {txt}log type:  {res}smcl
 {txt}opened on:  {res}13 Dec 2025, 11:05:01
{txt}
{com}. timer on 1
{txt}
{com}. 
. clear
{txt}
{com}. 
. ** work in the data directory until the end of this file
. cd "../data"
{res}C:\Users\hilar\Box\lights_2022\replication package\data
{txt}
{com}.  
. ras2dta, file(Cropland2000) genxcoord(x) genycoord(y)  replace

{txt}{hline 30}
No of {res}columns{txt}: {col 20}{res}        720
{txt}No of {res}rows{txt}: {col 20}{res}        270
{txt}number of {res}cells{txt}: {col 20}{res}    194,400
{txt}{p 0 4 2}
file {bf}
Cropland2000.dta{rm}
saved
{p_end}

{com}. summarize Cropland2000, detail

                        {txt}Cropland2000
{hline 61}
      Percentiles      Smallest
 1%    {res}        0              0
{txt} 5%    {res}        0              0
{txt}10%    {res}        0              0       {txt}Obs         {res}     61,760
{txt}25%    {res}        0              0       {txt}Sum of wgt. {res}     61,760

{txt}50%    {res} .0039172                      {txt}Mean          {res} .1077557
                        {txt}Largest       Std. dev.     {res} .1924752
{txt}75%    {res} .1222221              1
{txt}90%    {res} .4065993              1       {txt}Variance      {res} .0370467
{txt}95%    {res}  .584955              1       {txt}Skewness      {res} 2.109919
{txt}99%    {res} .7952587              1       {txt}Kurtosis      {res} 6.780507
{txt}
{com}. rename Cropland2000 cropland
{res}{txt}
{com}. label var cropland "Cropland share in 2000"
{txt}
{com}. keep x y cropland
{txt}
{com}. sort y x 
{txt}
{com}. save cropland2000, replace
{txt}{p 0 4 2}
file {bf}
cropland2000.dta{rm}
saved
{p_end}

{com}. 
. /*
> *Download files from
> *https://sedac.ciesin.columbia.edu/downloads/data/gpw-v4/gpw-v4-national-identifier-grid-rev11/gpw-v4-national-identifier-grid-rev11_30_min_asc.zip 
> *and unzip (can't do within Stata because login required)
> * ASCII file renamed country_grid_30min 
> * need package "kountry" to get country identifiers for later merges
> */
. 
. **# gridded country data from GPW
. clear
{txt}
{com}. ras2dta, file(country_grid_30min) genxcoord(x) genycoord(y)  replace 

{txt}{hline 30}
No of {res}columns{txt}: {col 20}{res}        720
{txt}No of {res}rows{txt}: {col 20}{res}        360
{txt}number of {res}cells{txt}: {col 20}{res}    259,200
{txt}{p 0 4 2}
file {bf}
country_grid_30min.dta{rm}
saved
{p_end}

{com}. drop if y<31 | y>300
{txt}(64,800 observations deleted)

{com}. replace y=y-30
{txt}(194,400 real changes made)

{com}. replace country_grid_30min=. if country_grid_30min==32767
{txt}(127,703 real changes made, 127,703 to missing)

{com}. kountry country_grid_30min, from(iso3n) to(iso3c)
{txt}(23 observations deleted)

{hline 44}
You are converting from {com}iso3n{txt} to {com}iso3c{txt}....
{hline 44}

{hline 38}
The command has finished.
The new variable is named {com}_ISO3C_{txt}.
{hline 38}

{com}. rename _ISO3C_ country_grid
{res}{txt}
{com}. label var country_grid "Country (ISO3c)"
{txt}
{com}. keep y x country_grid
{txt}
{com}. sort y x
{txt}
{com}. save country_grid_30min, replace
{txt}{p 0 4 2}
file {bf}
country_grid_30min.dta{rm}
saved
{p_end}

{com}. 
. 
. 
. 
. 
. 
. 
. **# Irrigation (from FAO Aquastats)
. ** extent is complete 90 N to 90 S 
. ** Data in 5 minutes, so each degree is 12 cells,
. *** Dropping 15 degrees from top is 15*12=180 cells from top and obs below (90+60)*12=1800
. 
. 
. copy "https://firebasestorage.googleapis.com/v0/b/fao-aquastat.appspot.com/o/GIS%2Fgmia_v5_aeigw_pct_aei_asc.zip?alt=media&token=ee5bd6d1-c8e2-44fd-a4cf-58d7563abbf6" irrig_gw.zip, replace
{txt}{p 0 4 2}
(file {bf}
irrig_gw.zip{rm}
not found)
{p_end}

{com}. copy "https://firebasestorage.googleapis.com/v0/b/fao-aquastat.appspot.com/o/GIS%2Fgmia_v5_aeisw_pct_aei_asc.zip?alt=media&token=38b7da06-ddfa-4500-90df-2db65e27c9a7" irrig_sw.zip, replace
{txt}{p 0 4 2}
(file {bf}
irrig_sw.zip{rm}
not found)
{p_end}

{com}. 
. foreach type in gw sw {c -(}
{txt}  2{com}. unzipfile irrig_`type'.zip, replace
{txt}  3{com}. 
. clear
{txt}  4{com}. ras2dta, file(gmia_v5_aei`type'_pct_aei) genxcoord(x) genycoord(y) dropmiss replace
{txt}  5{com}. sort y x
{txt}  6{com}. 
. tempfile i_`type'
{txt}  7{com}. save `i_`type''
{txt}  8{com}. erase irrig_`type'.zip
{txt}  9{com}. erase gmia_v5_aei`type'_pct_aei.asc
{txt} 10{com}. erase gmia_v5_aei`type'_pct_aei.dta
{txt} 11{com}. {c )-}
{res}{text:    inflating: gmia_v5_aeigw_pct_aei.asc}

{text:successfully unzipped irrig_gw.zip to current directory}
{text:total processed:  1}
{text:        skipped:  0}
{text:      extracted:  1}

{txt}{hline 30}
No of {res}columns{txt}: {col 20}{res}      4,320
{txt}No of {res}rows{txt}: {col 20}{res}      2,160
{txt}number of {res}cells{txt}: {col 20}{res}  9,331,200
{txt}{p 0 4 2}
(file {bf}
gmia_v5_aeigw_pct_aei.dta{rm}
not found)
{p_end}
{p 0 4 2}
file {bf}
gmia_v5_aeigw_pct_aei.dta{rm}
saved
{p_end}
{p 0 4 2}
file {bf}
C:\Users\hilar\AppData\Local\Temp\ST_457c_000001.tmp{rm}
saved
as .dta format
{p_end}
{res}{text:    inflating: gmia_v5_aeisw_pct_aei.asc}

{text:successfully unzipped irrig_sw.zip to current directory}
{text:total processed:  1}
{text:        skipped:  0}
{text:      extracted:  1}

{txt}{hline 30}
No of {res}columns{txt}: {col 20}{res}      4,320
{txt}No of {res}rows{txt}: {col 20}{res}      2,160
{txt}number of {res}cells{txt}: {col 20}{res}  9,331,200
{txt}{p 0 4 2}
(file {bf}
gmia_v5_aeisw_pct_aei.dta{rm}
not found)
{p_end}
{p 0 4 2}
file {bf}
gmia_v5_aeisw_pct_aei.dta{rm}
saved
{p_end}
{p 0 4 2}
file {bf}
C:\Users\hilar\AppData\Local\Temp\ST_457c_000002.tmp{rm}
saved
as .dta format
{p_end}

{com}. 
. copy "https://firebasestorage.googleapis.com/v0/b/fao-aquastat.appspot.com/o/GIS%2Fgmia_v5_aei_pct_asc.zip?alt=media&token=e448ce53-296f-4756-90c1-75c87f74e569" irrig_all.zip, replace
{txt}{p 0 4 2}
(file {bf}
irrig_all.zip{rm}
not found)
{p_end}

{com}. unzipfile irrig_all.zip, replace
{res}{text:    inflating: gmia_v5_aei_pct.asc}

{text:successfully unzipped irrig_all.zip to current directory}
{text:total processed:  1}
{text:        skipped:  0}
{text:      extracted:  1}
{txt}
{com}. clear
{txt}
{com}. ras2dta, file(gmia_v5_aei_pct) genxcoord(x) genycoord(y) dropmiss replace

{txt}{hline 30}
No of {res}columns{txt}: {col 20}{res}      4,320
{txt}No of {res}rows{txt}: {col 20}{res}      2,160
{txt}number of {res}cells{txt}: {col 20}{res}  9,331,200
{txt}{p 0 4 2}
(file {bf}
gmia_v5_aei_pct.dta{rm}
not found)
{p_end}
{p 0 4 2}
file {bf}
gmia_v5_aei_pct.dta{rm}
saved
{p_end}

{com}. sort y x
{txt}
{com}. 
. merge 1:1 y x using `i_gw', keep(master match) nogen
{res}
{txt}{col 5}Result{col 33}Number of obs
{col 5}{hline 41}
{col 5}Not matched{col 30}{res}       8,790,241
{txt}{col 9}from master{col 30}{res}       8,790,241{txt}  
{col 9}from using{col 30}{res}               0{txt}  

{col 5}Matched{col 30}{res}         540,959{txt}  
{col 5}{hline 41}

{com}. merge 1:1 y x using `i_sw', keep(master match) nogen
{res}
{txt}{col 5}Result{col 33}Number of obs
{col 5}{hline 41}
{col 5}Not matched{col 30}{res}       8,790,241
{txt}{col 9}from master{col 30}{res}       8,790,241{txt}  
{col 9}from using{col 30}{res}               0{txt}  

{col 5}Matched{col 30}{res}         540,959{txt}  
{col 5}{hline 41}

{com}. 
. 
. drop if y<181 | y>1800
{txt}(2,332,800 observations deleted)

{com}. replace y=y-180
{txt}(6,998,400 real changes made)

{com}. 
. 
. 
. *GW and SW as share of irrigated area. Multiply to make it share of total land
. gen igw=0 if gmia_v5_aei_pct==0
{txt}(540,959 missing values generated)

{com}. gen isw=0 if gmia_v5_aei_pct==0
{txt}(540,959 missing values generated)

{com}. replace igw=(gmia_v5_aeigw_pct_aei*gmia_v5_aei_pct)/10000 if gmia_v5_aei_pct>0 & !missing(gmia_v5_aei_pct)
{txt}(540,959 real changes made)

{com}. replace isw=(gmia_v5_aeisw_pct_aei*gmia_v5_aei_pct)/10000 if gmia_v5_aei_pct>0 & !missing(gmia_v5_aei_pct)
{txt}(540,959 real changes made)

{com}. 
. 
. * aggregate from 5 min (=.083 degree) to .5 degree
. gen x_6=floor((x-1)/6) + 1
{txt}
{com}. gen y_6=floor((y-1)/6) + 1 
{txt}
{com}. 
. collapse (mean) irrig=gmia_v5_aei_pct irrig_gw=igw irrig_sw=isw, by(x_6 y_6)
{res}{txt}
{com}. 
. *share, not percent
. replace irrig=irrig/100
{txt}(28,416 real changes made)

{com}. 
. label var irrig "Irrigation (share all land)"
{txt}
{com}. label var irrig_gw "Groundwater irrigation (share all land)"
{txt}
{com}. label var irrig_sw "Groundwater irrigation (share all land)"
{txt}
{com}. 
. 
. 
. rename x_6 x
{res}{txt}
{com}. rename y_6 y
{res}{txt}
{com}. 
. sort x y 
{txt}
{com}. compress
  {txt}variable {bf}{res}x{sf}{txt} was {bf}{res}float{sf}{txt} now {bf}{res}int{sf}
  {txt}variable {bf}{res}y{sf}{txt} was {bf}{res}float{sf}{txt} now {bf}{res}int{sf}
{txt}  (777,600 bytes saved)

{com}. save irrigation, replace
{txt}{p 0 4 2}
file {bf}
irrigation.dta{rm}
saved
{p_end}

{com}. erase irrig_all.zip
{txt}
{com}. erase gmia_v5_aei_pct.asc
{txt}
{com}. erase gmia_v5_aei_pct.dta
{txt}
{com}. 
. 
. 
. **# Pfaf4 grid
. 
. *Grid from Hydro1k shapefiles of level 4 subbasins 
. clear
{txt}
{com}. ras2dta, file(pfaf4_id) genxcoord(x) genycoord(y) dropmiss

{txt}{hline 30}
No of {res}columns{txt}: {col 20}{res}      7,200
{txt}No of {res}rows{txt}: {col 20}{res}      2,700
{txt}number of {res}cells{txt}: {col 20}{res}   19440000
{txt}{p 0 4 2}
file {bf}
pfaf4_id.dta{rm}
saved
{p_end}

{com}. rename pfaf4_id conpfaf4
{res}{txt}
{com}. sort x y
{txt}
{com}. save pfaf4_grid, replace
{txt}{p 0 4 2}
file {bf}
pfaf4_grid.dta{rm}
saved
{p_end}

{com}. erase pfaf4_id.dta
{txt}
{com}. 
. 
. **# Admin 1 data in .05 deg grid
. *Grid from GADM shapefiles of level 1 admin units 
. clear
{txt}
{com}. ras2dta, file(admin1) genxcoord(x) genycoord(y) replace 

{txt}{hline 30}
No of {res}columns{txt}: {col 20}{res}      7,200
{txt}No of {res}rows{txt}: {col 20}{res}      2,700
{txt}number of {res}cells{txt}: {col 20}{res}   19440000
{txt}{p 0 4 2}
(file {bf}
admin1.dta{rm}
not found)
{p_end}
{p 0 4 2}
file {bf}
admin1.dta{rm}
saved
{p_end}

{com}. sort x y
{txt}
{com}. save admin1, replace
{txt}{p 0 4 2}
file {bf}
admin1.dta{rm}
saved
{p_end}

{com}. 
. 
. *Convert flares data into grid
. 
. import excel "https://eogdata.mines.edu/global_flare_data/VIIRS_Global_flaring_d.7_slope_0.0298_2012-2016_web.xlsx", sheet("flares_upstream") firstrow case(lower) clear
{res}{text}(23 vars, 21,387 obs)

{com}. 
. tempfile upstream
{txt}
{com}. save `upstream'
{txt}{p 0 4 2}
file {bf}
C:\Users\hilar\AppData\Local\Temp\ST_457c_000003.tmp{rm}
saved
as .dta format
{p_end}

{com}. 
. import excel "https://eogdata.mines.edu/global_flare_data/VIIRS_Global_flaring_d.7_slope_0.0298_2012-2016_web.xlsx", sheet("flares_downstream_oil") firstrow case(lower) clear
{res}{text}(23 vars, 958 obs)

{com}. 
. tempfile do
{txt}
{com}. save `do'
{txt}{p 0 4 2}
file {bf}
C:\Users\hilar\AppData\Local\Temp\ST_457c_000004.tmp{rm}
saved
as .dta format
{p_end}

{com}. 
. import excel "https://eogdata.mines.edu/global_flare_data/VIIRS_Global_flaring_d.7_slope_0.0298_2012-2016_web.xlsx", sheet("flares_downstream_gas") firstrow case(lower) clear
{res}{text}(23 vars, 68 obs)

{com}. 
. append using `upstream' `do'
{txt}{p 0 7 2}
(variable
{bf:iso_code} was {bf:str3}, now {bf:str8} to accommodate using data's values)
{p_end}
{p 0 7 2}
(variable
{bf:country} was {bf:str20}, now {bf:str32} to accommodate using data's values)
{p_end}

{com}. 
. gen int y=int((90-latitude)/.05)+1
{txt}
{com}. gen int x=int((180+longitude)/.05)+1
{txt}
{com}. 
. 
. sort y x
{txt}
{com}. 
. collapse (firstnm) type (mean) detection_frequency_2012 detection_frequency_2016, by(y x)
{res}{txt}
{com}. 
. *drop first 15 degrees (=15*20 cells)
. drop if y<301 | y>3100
{txt}(0 observations deleted)

{com}. replace y=y-300
{txt}(15,178 real changes made)

{com}. 
. label var type "Flare type"
{txt}
{com}. label var detection_frequency_2012 "Flare detection freq (2012)"
{txt}
{com}. label var detection_frequency_2016 "Flare detection freq (2016)"
{txt}
{com}. 
. label data "Flare frequency (cell only present if flare indicated, rest zeros)"
{txt}
{com}. 
. save flares, replace
{txt}{p 0 4 2}
(file {bf}
flares.dta{rm}
not found)
{p_end}
{p 0 4 2}
file {bf}
flares.dta{rm}
saved
{p_end}

{com}. 
. 
. **# Global Rural Urban Mapping
. *Download files from "https://sedac.ciesin.columbia.edu/gpw/app/?code=ECU&file=grumpv1&data=urextent&type=ascii&resolut=30" and unzip
. *takes a long time to read in
. * file exceeds Dataverse size limits and is not provided
. clear
{txt}
{com}. cd gl_grumpv1_urextent_ascii_30
{res}C:\Users\hilar\Box\lights_2022\replication package\data\gl_grumpv1_urextent_ascii_30
{txt}
{com}. ras2dta, file(glurextents) genxcoord(x) genycoord(y) dropmiss replace

{txt}{hline 30}
No of {res}columns{txt}: {col 20}{res}     43,200
{txt}No of {res}rows{txt}: {col 20}{res}     16,800
{txt}number of {res}cells{txt}: {col 20}{res}  725760000
{txt}{p 0 4 2}
(file {bf}
glurextents.dta{rm}
not found)
{p_end}
{p 0 4 2}
file {bf}
glurextents.dta{rm}
saved
{p_end}

{com}. * extent is 85 degree N to -58 degrees S
. * lights and droughts from 75 degrees N to -60 S so drop 10 degrees 
. ** each obs is 30 sec, so 60*2 obs per degree
. 
. use glurextents, clear
{txt}
{com}. drop if y<1201 | y>18000
{txt}(5,881,902 observations deleted)

{com}. replace y=y-1200
{txt}(206,038,184 real changes made)

{com}. 
. * aggregate from 30 sec to .05 degree
. gen x_6=floor((x-1)/6) + 1
{txt}
{com}. gen y_6=floor((y-1)/6) + 1 
{txt}
{com}. 
. gen urban=(glurextents==2) if !missing(glurextents)
{txt}
{com}. 
. * characterize as urban if anywhere in .05 is urban 
. collapse (max) urban (mean) urbanshare=urban, by(x_6 y_6)
{res}{txt}
{com}. 
. label var urban "Urban (from GRUMP)"
{txt}
{com}. label var urbanshare "Urban share"
{txt}
{com}. 
. rename x_6 x
{res}{txt}
{com}. rename y_6 y
{res}{txt}
{com}. 
. sort x y 
{txt}
{com}. compress
  {txt}variable {bf}{res}x{sf}{txt} was {bf}{res}float{sf}{txt} now {bf}{res}int{sf}
  {txt}variable {bf}{res}y{sf}{txt} was {bf}{res}float{sf}{txt} now {bf}{res}int{sf}
  {txt}variable {bf}{res}urban{sf}{txt} was {bf}{res}float{sf}{txt} now {bf}{res}byte{sf}
{txt}  (40,963,594 bytes saved)

{com}. 
. *back in data directory
. cd ..
{res}C:\Users\hilar\Box\lights_2022\replication package\data
{txt}
{com}. save urban, replace
{txt}{p 0 4 2}
file {bf}
urban.dta{rm}
saved
{p_end}

{com}. erase "./gl_grumpv1_urextent_ascii_30/glurextents.dta"
{txt}
{com}. 
. 
. cd "../code"
{res}C:\Users\hilar\Box\lights_2022\replication package\code
{txt}
{com}. 
. 
. timer off 1
{txt}
{com}. timer list 1
{txt}
{com}. 
. log close
      {txt}name:  {res}<unnamed>
       {txt}log:  {res}C:\Users\hilar\Box\lights_2022\replication package\code\read_other_rasters.smcl
  {txt}log type:  {res}smcl
 {txt}closed on:  {res}13 Dec 2025, 11:19:19
{txt}{.-}
{smcl}
{txt}{sf}{ul off}